<?php

namespace app\sms\service\platform\aliyun;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use app\admin\service\BaseService;
use app\sms\model\SmsLogModel;
use Darabonba\OpenApi\Models\Config;

class Helper extends BaseService{

    protected static $accessKeyId;
    protected static $accessKeySecret;
    protected static $signName;
    protected static $templateCode;
    protected static $templateParam;

    public function __construct($config = [])
    {
        if($config) {
            self::$accessKeyId = $config['access_id'];
            self::$accessKeySecret = $config['access_key'];
            self::$signName = $config['sign'];
            self::$templateCode = $config['templateCode'];
            self::$templateParam = $config['templateParam'] ? json_encode($config['templateParam'], true) : '';
        }
    }

    /**
     * 发送短信
     *
     * @Author tanruizheng
     * @DateTime 2021-08-26
     * @param [type] $args
     * @return void
     */
    public static function sendSms($phoneNumbers, $areaCode = 86){
        try {
            if(empty(self::$accessKeyId)) return self::createFail('access_id不能为空');
            if(empty(self::$accessKeySecret)) return self::createFail('access_key不能为空');
            if(empty(self::$signName)) return self::createFail('sign不能为空');
            if(empty($phoneNumbers)) return self::createFail('手机号不能为空');
            if(empty(self::$templateCode)) return self::createFail('template不能为空');
            
            $config = new Config([
                // 您的AccessKey ID
                "accessKeyId" => self::$accessKeyId,
                // 您的AccessKey Secret
                "accessKeySecret" => self::$accessKeySecret
            ]);
            // 访问的域名
            $config->endpoint = "dysmsapi.aliyuncs.com";
            $client =  new Dysmsapi($config);
            
            $sendSmsRequest = new SendSmsRequest([
                "phoneNumbers"  => $phoneNumbers,
                "signName"      => self::$signName,
                "templateCode"  => self::$templateCode,
                "templateParam" => self::$templateParam,
            ]);

            //记录日志
            $smsLog = new SmsLogModel();
            $smsLog->operator = 'aliyun';
            $smsLog->template = self::$templateCode;
            $smsLog->recv = $phoneNumbers;
            $smsLog->param = self::$templateParam;
            $smsLog->sendtime = time();
            $smsLog->area_code = $areaCode;

            $res = $client->sendSms($sendSmsRequest);

            $smsLog->result = $res->body->message;
            $smsLog->save();

            $code = $res->body->code;
            if($code === 'OK'){
                return self::createSuccess('发送成功');
            }else{
                return self::createFail($res->body->message);
            }
        } catch (\Exception $e) {
            $smsLog->result = $e->getMessage();
            $smsLog->save();
            return self::createFail($e->getMessage());
        }
    }
}